/**
 * 
 */
/**
 * @author dell
 *
 */
package com.p2p.wallet.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.account.po.Account;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.MD5Encryption;
import com.p2p.common.utils.StringUtils;
import com.p2p.port.po.MonitorPort;
import com.p2p.search.po.Netbar;
import com.p2p.search.service.SearchService;
import com.p2p.search.vo.SelectUser;
import com.p2p.user.controller.PermissionController;
import com.p2p.user.po.Role;
import com.p2p.user.po.User;
import com.p2p.user.po.UserRole;
import com.p2p.user.service.PermissionService;
import com.p2p.user.vo.PermissionVo;
import com.p2p.user.vo.UserRoleVo;
import com.p2p.wallet.po.Wallet;
import com.p2p.wallet.service.WalletService;

@RestController
@RequestMapping(value = "wallet")
public class WalletController {

	private static final Logger logger = LoggerFactory.getLogger(WalletController.class);
	@Autowired
	private WalletService walletService;

	@RequestMapping(value = "selectWalletOption", method = RequestMethod.GET)
	public Message selectWelletOption(HttpServletRequest request) {

		List<Wallet> portList = walletService.selectList(new EntityWrapper<Wallet>().eq("status", 1));
		
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), portList);
	}

	@RequestMapping(value = "insertWallet", method = RequestMethod.POST)
	public Message insertWallet(HttpServletRequest request, Wallet wallet) {

		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.out.println("名字为" + wallet.getName());
		wallet.setAgentCount(0);
		wallet.setStatus(1);
		wallet.setCreateTime(timestamp);
		walletService.insert(wallet);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	@RequestMapping(value = "updateWallet", method = RequestMethod.POST)
	public Message updateWallet(HttpServletRequest request, Wallet wallet) {

		walletService.updateById(wallet);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 逻辑删除用户
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteWallet", method = RequestMethod.GET)
	public Message deleteWallet(HttpServletRequest request) {
		Long Id = Long.parseLong(request.getParameter("walletid"));
		Wallet wallet = walletService.selectById(Id);
		wallet.setStatus(-1);
		walletService.updateById(wallet);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 获取用户 By UserId
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectWalletById", method = RequestMethod.GET)
	public Message selectWalletById(HttpServletRequest request) {
		Long id = Long.parseLong(request.getParameter("id"));
		Wallet wallet = walletService.selectById(id);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), wallet);
	}

	@RequestMapping(value = "selectWallet", method = RequestMethod.GET)
	public Map selectWallet(HttpServletRequest request, Wallet wallet) {
		System.out.println("调用了查询接口");
		EntityWrapper<Wallet> userWrapper = new EntityWrapper<Wallet>();
		userWrapper.eq("status", 1);
		if (wallet.getGroupId() != null) {
			userWrapper.where("group_id= {0}", wallet.getGroupId());
		}
		List<Wallet> userList = walletService.selectList(userWrapper);
		Map result = new HashMap();
		if (userList.size() > 0) {
			for (int i = 0; i < userList.size(); i++) {

				Wallet wallet1 = userList.get(i);
				List<Account>  list = walletService.selectagentid("'"+wallet1.getName()+"'");
				
					wallet1.setAgentCount(list.size());
				
				
				userList.set(i, wallet1);
			}
		}
		result.put("rel", true);
		result.put("msg", "SUCCESS");
		result.put("list", userList);
		result.put("count", userList.size());

		return result;
	}

}